import { stringify } from 'qs';
import request, { fingerprintFetch } from '@/utils/request';
import { withProcessedData } from '@/utils/marketingMap/utils';
import { message } from 'antd';
import { downloadArrayBufferFile, getMd5, getToken } from '@/utils';

const basePath = '/msmarketingmap/market/expansion/residency';

// 接口调整，删除
// export const fetchStore = async (query: QueryProps) =>
//   withProcessedData(query, async query => {
//     const result = await request(
//       `/msmarketingmap/aftersales/overall/store/list?${stringify(query)}`,
//     );

//     if (result.statusCode !== '0') return [];

//     if (!result.data?.length) {
//       message.info('无法获取经销商位置，请稍后再试');
//     }

//     return result.data;
//   });

// export const fetchHeatmap = async (query: QueryProps) =>
//   withProcessedData(query, async query => {
//     const result = await request(
//       `/msmarketingmap/aftersales/overall/heatmap?heatType=1&${stringify(query)}`,
//     );

//     if (result.statusCode !== '0') return [];

//     return result.data;
//   });

// 接口调整，删除
// export const fetchMapAll = (query: QueryProps) => {
//   return Promise.all([fetchStore(query), fetchHeatmap(query)]);
// };

// 常驻地-在哪外拓市场
export const fetchOverview = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/overview?${stringify(query)}`);

    if (result.statusCode !== '0') return {};

    return result.data;
  });

// 车系分布
export const fetchPersonaSeries = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/persona/series?${stringify(query)}`);

    if (result.statusCode !== '0') return [];

    return result.data;
  });

// 专营店分布
export const fetchPersonaStore = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/persona/store?${stringify(query)}`);

    if (result.statusCode !== '0') return [];

    return result.data;
  });
// 距离分布
export const fetchPersonaDistance = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/persona/distance?${stringify(query)}`);

    if (result.statusCode !== '0') return [];

    return result.data;
  });
export const fetchPersonaPortraitInfo = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/portrait/info?${stringify({ ...query, tags: 4 })}`);

    if (result.statusCode !== '0') return [];

    return result.data;
  });
export const fetchPersona = (query: QueryProps) => {
  return Promise.all([
    fetchPersonaSeries(query),
    fetchPersonaStore(query),
    fetchPersonaDistance(query),
    fetchPersonaPortraitInfo(query),
  ]);
};

// 导出
export const exportVehicle = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const params = stringify(query);
    const url = `${window.g.url}${basePath}/export?${params}`;

    fingerprintFetch(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json;charset=UTF-8',
        OptAuthorization: `${getToken()}`,
        shaSign: getMd5(params),
      },
    })
      .then(async response => response.blob()) // 转换响应为blob
      .then((blob: any) => {
        if (!blob.size) {
          message.error('当前条件下，暂无数据');
          return;
        }

        downloadArrayBufferFile(blob, '常驻地排名.xlsx');
      });
  });
